/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.bytedance.scenedemo.animatiom;

import com.bytedance.scene.dialog.bottomsheet.ValueAnimator;
import com.bytedance.scene.group.GroupScene;
import com.bytedance.scenedemo.ResourceTable;
import ohos.aafwk.content.Intent;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorGroup;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.*;

/**
 * TransitionDemo1.
 *
 * @since 2021-06-17
 */
public class TransitionDemo1 extends GroupScene {
    private static final int BTN_AND_IMAGE_DURATION = 2000;
    private static final int BTN_ROTATE_DURATION = 1000;
    private static final int IMAGE_WIDTH_REDUCE_DURATION = 100;
    private static final int SECOND = 2;
    private static final int FIVE = 5;
    private static final int TWENTY = 20;
    private static final int TWO_HUNDRED = 200;
    private ComponentContainer componentContainer;

    @Override
    public ComponentContainer onCreateView(LayoutScatter inflater, ComponentContainer container,
                                           Intent savedInstanceState) {
        componentContainer = (ComponentContainer) inflater.parse(ResourceTable.Layout_transtion_demo1,
                container, false);
        return componentContainer;
    }

    @Override
    public void onResume() {
        super.onResume();
        int height = getActivity().getWindow().getLayoutConfig().get().height;
        int width = getActivity().getWindow().getLayoutConfig().get().width;
        Button btn = (Button) componentContainer.findComponentById(ResourceTable.Id_btn);
        AnimatorGroup animatorGroup = getAnimatorGroup(height, width, btn);


        Button btnNothingDoWithRight = (Button) componentContainer.findComponentById(
                ResourceTable.Id_btn_nothing_do_with_right);
        AnimatorProperty animatorPropertyRight = btnNothingDoWithRight.createAnimatorProperty();
        animatorPropertyRight.moveToY(height).setDuration(BTN_AND_IMAGE_DURATION);


        Button btnNothingDoWithLeft = (Button) componentContainer.findComponentById(
                ResourceTable.Id_btn_nothing_do_with_left);
        AnimatorProperty animatorPropertyLeft = btnNothingDoWithLeft.createAnimatorProperty();
        animatorPropertyLeft.moveToY(0).setDuration(BTN_AND_IMAGE_DURATION);


        StackLayout slRed = (StackLayout) componentContainer.findComponentById(ResourceTable.Id_sl_red);
        Image imageBlue = (Image) componentContainer.findComponentById(ResourceTable.Id_image_blue);

        btnNothingDoWithLeft.setVisibility(Component.VISIBLE);

        btnNothingDoWithRight.getComponentTreeObserver().addWindowBoundListener(new CustomWindowBoundListener() {
            @Override
            public void onWindowBound() {
                animatorPropertyRight.start();
            }
        });

        btnNothingDoWithLeft.getComponentTreeObserver().addWindowBoundListener(new CustomWindowBoundListener() {
            @Override
            public void onWindowBound() {
                animatorPropertyLeft.start();
            }
        });

        btn.getComponentTreeObserver().addWindowBoundListener(new CustomWindowBoundListener() {
            @Override
            public void onWindowBound() {
                animatorGroup.start();
            }
        });

        setRedAnimation(height, slRed, imageBlue);
    }

    private void setRedAnimation(int height, StackLayout slRed, Image imageBlue) {
        float scale = (float) slRed.getWidth() / (float) (imageBlue.getWidth());
        AnimatorProperty imageAnimatorProperty1 = imageBlue.createAnimatorProperty();
        imageAnimatorProperty1
                .scaleX(scale).scaleY(scale)
                .moveByY(0).moveToY(height - imageBlue.getHeight() * SECOND)
                .setDuration(BTN_AND_IMAGE_DURATION);

        AnimatorProperty imageAnimatorProperty2 = imageBlue.createAnimatorProperty();
        AnimatorGroup imageAnimatorGroup = new AnimatorGroup();
        slRed.getComponentTreeObserver().addWindowBoundListener(new ComponentTreeObserver.WindowBoundListener() {
            @Override
            public void onWindowBound() {
                imageAnimatorProperty2
                        .rotate(0)
                        .moveToX(slRed.getContentPositionX()
                                + (imageBlue.getWidth() * scale - imageBlue.getWidth()) / SECOND - FIVE)
                        .moveToY(slRed.getContentPositionY()
                                + (imageBlue.getHeight() * scale - imageBlue.getHeight()) / SECOND)
                        .setDuration(BTN_AND_IMAGE_DURATION);
                imageAnimatorGroup.runSerially(imageAnimatorProperty1, imageAnimatorProperty2);
                imageAnimatorGroup.setStateChangedListener(new StateChangedListener() {
                    @Override
                    public void onEnd(Animator animator) {
                        ValueAnimator valueAnimator1 = ValueAnimator.ofFloat();
                        valueAnimator1.setValueUpdateListener((animatorValue, v) ->
                                imageBlue.setWidth(imageBlue.getWidth() - 1));
                        valueAnimator1.setDuration(IMAGE_WIDTH_REDUCE_DURATION);
                        valueAnimator1.start();
                    }
                });
                imageAnimatorGroup.start();
            }

            @Override
            public void onWindowUnbound() {

            }
        });
    }

    private AnimatorGroup getAnimatorGroup(int height, int width, Button btn) {
        AnimatorProperty btnAnimatorProperty1 = btn.createAnimatorProperty();
        int moveY = (height - btn.getHeight()) / SECOND;
        int moveX = (width - btn.getWidth() - TWO_HUNDRED) / SECOND;
        btnAnimatorProperty1.moveToY(moveY)
                .moveToX(moveX)
                .setDuration(BTN_AND_IMAGE_DURATION);
        AnimatorProperty btnAnimatorProperty2 = btn.createAnimatorProperty();
        btnAnimatorProperty2.rotate(TWENTY).setDuration(BTN_ROTATE_DURATION);
        AnimatorGroup animatorGroup = new AnimatorGroup();
        btn.setText("打开 SCENE，已点击");
        ValueAnimator valueAnimator = ValueAnimator.ofFloat();
        valueAnimator.setValueUpdateListener((animatorValue, v) -> btn.setWidth(1 + btn.getWidth()));
        valueAnimator.setDuration(BTN_AND_IMAGE_DURATION);

        AnimatorGroup.Builder animatorGroupBuilder = animatorGroup.build();
        animatorGroupBuilder.addAnimators(btnAnimatorProperty1, valueAnimator).addAnimators(btnAnimatorProperty2);
        return animatorGroup;
    }

    /**
     * CustomWindowBoundListener.
     *
     * @since 2021-06-17
     */
    private static class CustomWindowBoundListener implements ComponentTreeObserver.WindowBoundListener {
        @Override
        public void onWindowBound() {

        }

        @Override
        public void onWindowUnbound() {

        }
    }

    /**
     * StateChangedListener.
     *
     * @since 2021-06-17
     */
    private static class StateChangedListener implements Animator.StateChangedListener {
        @Override
        public void onStart(Animator animator) {

        }

        @Override
        public void onStop(Animator animator) {

        }

        @Override
        public void onCancel(Animator animator) {

        }

        @Override
        public void onEnd(Animator animator) {

        }

        @Override
        public void onPause(Animator animator) {

        }

        @Override
        public void onResume(Animator animator) {

        }
    }
}
